﻿using System.Collections;
using UnityEngine;

namespace PathFinding
{
    public class AAObstaleBox : MonoBehaviour
    {
        public PFGrid Grid;
        public AABBObstacle Obstacle;
        public Color DebugColor = Color.red;
        public Vector2Int TL, BR;

        MeshRenderer meshRenderer;
        public void Awake()
        {
            Vector2 center = new Vector2(transform.position.x, transform.position.z);
            Vector2 halfsize = new Vector2(Mathf.Abs(transform.localScale.x / 2), Mathf.Abs(transform.localScale.z / 2));
            Obstacle = new AABBObstacle(Grid, center, halfsize);
            meshRenderer = this.GetComponent<MeshRenderer>();
        }

        // Use this for initialization
        void Start()
        {
            Grid.AddObstacle(Obstacle);
        }

        // Update is called once per frame
        void Update()
        {
            TL = Obstacle.TopLeft;
            BR = Obstacle.BottomRight;
        }

        private void OnDrawGizmos()
        {
            if (Grid != null && Obstacle != null)
            {
                if (meshRenderer)
                    meshRenderer.enabled = false;
                Gizmos.color = Grid.ObstacleColor;
                Obstacle.DebugDraw(DebugColor);
            }
            else if (meshRenderer)
                meshRenderer.enabled = true;

        }
    }
}